<?php

$appid = "wxde4599bcc831b168";

$secret = "abf2ee0c9827937179431c3f19a6d32e";

$code = $_GET["code"];

$result = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";

$result = sprintf($result,$appid,$secret,$code);

$data = https_request($result);

//file_put_contents("get.log",var_export($data,true));

$data = json_decode($data,true);

$openid = $data["openid"];

$token = md5(date("YmdHis",time()));

echo json_encode(["code" => 1 , "data" => $openid ,"token" => $token]);

//提交菜单内容给服务器
function https_request($url,$data = null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}